'use strict';
let Response = require('../../model/Response'),
    RestifyProxy = require('../../util/RestifyProxy');

let _appendChildren = function (nodes) {
    let html = [];
    html.push(`<ul module-menu="">`);
    for (let i in nodes) {
        html.push(`<li module-menu="${nodes[i].url}"
                    module-target="page">${nodes[i].name}`);
        if (nodes[i].children && nodes[i].children.length > 0) {
            html.push(_appendChildren(nodes[i].children));
        }
        html.push('</li>');
    }
    html.push('</ul>');
    return html.join('');
};
let _appendNodes = function (menuArray) {
    let html = [];
    for (let i in menuArray) {
        html.push(`<div class="sub noprint"
            module-menu="${menuArray[i].url}"
             module-target="page">
            <div class="menu-icon organization"></div>
            <span class="text">${menuArray[i].name}</span>`);
        if (menuArray[i].children && menuArray[i].children.length > 0) {
            html.push(_appendChildren(menuArray[i].children));
        }
        html.push(`</div>`);
    }
    return html.join('');
};


/**
 * 后台主页面
 */
module.exports.backPage = function () {
    let req = this.req;
    let user = req.user;
     user.menus=[
         {
             "id": 201,
             "parentId": -1,
             "code": "pallet-home",
             "name": "首页",
             "icon": "",
             "style": "homepage",
             "url": "/back/homepage/page",
             "level": 1
         },
         {
             "id": 131,
             "parentId": -1,
             "code": "Bootstrap",
             "name": "数据表格",
             "icon": "",
             "style": "Bootstrap",
             "url": "",
             "level": 1
         },
         {
             "id": 132,
             "parentId": 131,
             "code": "account",
             "name": "Bootstrap-table",
             "icon": "",
             "style": "",
             "url": "/back/Bootstrap/Bootstrap-table/page",
             "level": 2
         },
         {
             "id": 285,
             "parentId": -1,
             "code": "statistic-analysis",
             "name": "统计图表",
             "icon": "",
             "style": "statistic",
             "url": "",
             "level": 1
         },
         {
             "id": 283,
             "parentId": 285,
             "code": "cooperation-statistic",
             "name": "柱状图统计",
             "icon": "",
             "style": "",
             "url": "/back/statistic/cooperation/page",
             "level": 2
         },
         {
             "id": 282,
             "parentId": 285,
             "code": "agent-statistic",
             "name": "饼图统计",
             "icon": "",
             "style": "",
             "url": "/back/statistic/agent/page",
             "level": 2
         },
        ]
    let menusNodes = JSON.parse(JSON.stringify(user.menus));
    let menuArray = [];
    let menuMap = {};
    for (let i = 0, l = menusNodes.length; i < l; i++) {
        menuMap[menusNodes[i].id] = menusNodes[i];
    }
    for (let i = 0, l = menusNodes.length; i < l; i++) {
        if (menuMap[menusNodes[i].parentId] && menusNodes[i].id != menusNodes[i].parentId) {
            if (!menuMap[menusNodes[i].parentId].children)
                menuMap[menusNodes[i].parentId].children = [];
            menuMap[menusNodes[i].parentId].children.push(menusNodes[i]);
        } else {
            menuArray.push(menusNodes[i]);
        }
    }
    let menuHtml = _appendNodes(menuArray);
    let reqData = {user, menuHtml};
    let response = new Response();
    response.render('back/back', reqData);
    this.resolve(response);
};